package com.simpay.cashierapptest;

import ru.qiwi.security.Security;
import lib.Core;
import Core.Contants;
import Core.GlobalData;
import DTO.Auth;
import DTO.Client;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener {
	private Button login;
	private EditText tfUsername;
	private EditText tfPassword;
	private EditText tfTerminal;
	public static LoginActivity instanceLoginActivity = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		Core.context = LoginActivity.this;
		Core.context_private = LoginActivity.this;
		login = (Button) findViewById(R.id.loginbutton);
		login.setOnClickListener(this);

		tfUsername = (EditText) findViewById(R.id.username);
		tfPassword = (EditText) findViewById(R.id.password);
		tfTerminal = (EditText) findViewById(R.id.terminal);

		tfTerminal.setText("9393292");
		tfUsername.setText("namtest");
		tfPassword.setText("");

		instanceLoginActivity = this;
	}

	private boolean loginWithInput() {

		if (tfTerminal.getText().length() <= 0
				|| tfUsername.getText().length() <= 0
				|| tfPassword.getText().length() <= 0) {

			Toast.makeText(LoginActivity.this, R.string.s1000,
					Toast.LENGTH_SHORT).show();
			tfPassword.setText("");
			return false;
		}

		GlobalData.auth = new Auth(tfPassword.getText().toString(), tfUsername
				.getText().toString(), "MD5");
		GlobalData.client = new Client(null, tfTerminal.getText().toString(),
				getString(R.string.s1001), getString(R.string.s1002));

		Core.writeData(Contants.AUTH, GlobalData.auth);
		Core.writeData(Contants.CLIENT, GlobalData.client);

		Core.writeData(Contants.SECURITY_KEY, null);
		Core.writeData(Contants.ISFIRSTSTART, null);
		Security.sec = null;
		Security.sec = new Security(null);

		return true;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		instanceLoginActivity = null;
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.loginbutton:
			loginWithInput();
			break;

		default:
			break;
		}

	}
}
